import { defineStore } from 'pinia'

export const useTabsStore = defineStore('tabs', {
  state: () => ({
    tabs: [],
    activeTab: null
  }),
  actions: {
    addTab(route) {
      const exists = this.tabs.some(tab => tab.path === route.path)
      if (!exists) {
        this.tabs.push({
          title: route.meta.title || '未命名标签',
          path: route.path,
          fullPath: route.fullPath,
          query: route.query,
          params: route.params
        })
      }
      this.activeTab = route.path
    },
    removeTab(path) {
      this.tabs = this.tabs.filter(tab => tab.path !== path)
      if (this.activeTab === path) {
        this.activeTab = this.tabs[this.tabs.length - 1]?.path || null
      }
    },
    setActiveTab(path) {
      this.activeTab = path
    }
  }
})